package com.lune.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lune.job.framework.redis.RedisUtil;
import com.lune.job.mapper.UserMapper;
import com.lune.job.pojo.ResPageBean;
import com.lune.job.pojo.RespBean;
import com.lune.job.pojo.User;
import com.lune.job.service.IUserService;
import com.lune.job.utils.UserThreadLocal;
import com.lune.job.vo.UserData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 获取所有用户分页
     *
     * @param currentPage
     * @param size
     * @param user
     * @return
     */
    @Override
    public ResPageBean getAllUserByPage(Integer currentPage, Integer size, User user) {
        //开启分页
        Page<User> page = new Page<>(currentPage, size);
        IPage<User> userIPage = userMapper.getAllUserByPage(page, user);
        ResPageBean resPageBean = new ResPageBean(userIPage.getTotal(),userIPage.getPages(), userIPage.getRecords());
        return resPageBean;
    }

    /**
     * 根据用户id查询
     *
     * @param id
     * @return
     */
    @Override
    public List<User> getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    /**
     * 登录方法
     * @param userName
     * @param password
     * @return
     */
    @Override
    public User login(String userName, String password) {

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("userName", userName);
        wrapper.eq("password",password);
        List<User> list = userMapper.selectList(wrapper);
        if (list!=null && list.size()>0){
            return  list.get(0);
        }else {
            return  null;
        }

    }

    /**
     * 条件查询用户
     * @param user
     * @return
     */
    @Override
    public List<User> queryByCon(User user) {
        return  userMapper.query(user);
    }


}
